﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using LowLevelGraphics;
using LowLevelGraphics.Filter;

namespace ImageRecognition2.Forms
{
    //TODO: FormLoad3dPic, Enable button functions to shift pics
    /// <summary>
    /// 
    /// </summary>
    public partial class FormLoad3dPic : Form
    {
        protected UnsafeBitmap m_BitmapLeft = null;
        protected UnsafeBitmap m_BitmapRight = null;
        protected UnsafeBitmap m_BitmapResult = null;

        protected Bitmap m_AnaglyphBitmapResult = null;

        protected string m_sLeft = string.Empty;
        protected string m_sRight = string.Empty;

        /// <summary>
        /// Initializes a new instance of the <see cref="FormLoad3dPic"/> class.
        /// </summary>
        public FormLoad3dPic()
        {
            InitializeComponent();
            foreach (AnaglyphMode anaglyphMode in Enum.GetValues(typeof(AnaglyphMode)))
            {
                if (anaglyphMode != AnaglyphMode.None)
                {
                    this.cobAnaglyphMode.Items.Add(anaglyphMode);
                }
            }

            this.cobAnaglyphMode.SelectedIndex = 0;

            this.pictureBoxLeft.BackgroundImageLayout = ImageLayout.Zoom;
            this.pictureBoxResult.BackgroundImageLayout = ImageLayout.Zoom;
        }

        /// <summary>
        /// Gets the bitmap result.
        /// </summary>
        /// <value>The bitmap result.</value>
        public Bitmap BitmapResult
        {
            get { return m_AnaglyphBitmapResult; }
        }

        /// <summary>
        /// Handles the Click event of the cbOK control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void cbOK_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.OK;
            this.Close();
        }

        /// <summary>
        /// Handles the Click event of the cbCancel control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void cbCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }


        /// <summary>
        /// Handles the SelectedIndexChanged event of the cobAnaglyphMode control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void cobAnaglyphMode_SelectedIndexChanged(object sender, EventArgs e)
        {
            //if (chkbPreview.Checked)
            //{
            //    if (chkbTwoPics.Checked)
            //    {
            //        ExtractAnaglyph(pictureBoxLeft.BackgroundImage, pictureBoxRight.BackgroundImage);
            //    }
            //    else
            //    {
            //        ExtractAnaglyph(pictureBoxLeft.BackgroundImage);
            //    }
            //}
        }

        /// <summary>
        /// Extracts the anaglyph.
        /// </summary>
        /// <param name="_unsafeBitmapLeft">The _unsafe bitmap left.</param>
        /// <param name="_unsafeBitmapRight">The _unsafe bitmap right.</param>
        public void ExtractAnaglyph(UnsafeBitmap _unsafeBitmapLeft, UnsafeBitmap _unsafeBitmapRight)
        {
            //Size size = m_Bitmap.Size;
            Size size = _unsafeBitmapLeft.Size;
            
            AnaglyphMode anaglyphMode = (AnaglyphMode)cobAnaglyphMode.SelectedItem;
            
            Anaglyph anaglyph = new Anaglyph();
            anaglyph.Mode = AnaglyphMode.Optimized;

            UnsafeBitmap anaglyphProcess = null;
            if (chkbSwap.Checked)
            {
                anaglyphProcess = anaglyph.Process(_unsafeBitmapRight, _unsafeBitmapLeft);
            }
            else
            {
                anaglyphProcess = anaglyph.Process(_unsafeBitmapLeft, _unsafeBitmapRight);
            }
            anaglyphProcess.UnlockImage();

            m_AnaglyphBitmapResult = anaglyphProcess.InternalBitmap;
            pictureBoxResult.BackgroundImage = anaglyphProcess.InternalBitmap;

            _unsafeBitmapLeft.UnlockImage();
            _unsafeBitmapRight.UnlockImage();
        }

        /// <summary>
        /// Extracts the anaglyph.
        /// </summary>
        public void ExtractAnaglyph(UnsafeBitmap _unsafeBitmap)
        {
            Size size = _unsafeBitmap.Size;
            _unsafeBitmap.UnlockImage();
            UnsafeBitmap bitmap1 = BitmapFunctions.GetPicturePart(_unsafeBitmap.InternalBitmap, new Rectangle(0, 0, size.Width / 2, size.Height));
            UnsafeBitmap bitmap2 = BitmapFunctions.GetPicturePart(_unsafeBitmap.InternalBitmap, new Rectangle(size.Width / 2, 0, size.Width, size.Height));

            UnsafeBitmap bitmapTemp = null;
            if (chkbSwap.Checked)
            {
                bitmapTemp = bitmap1;
                bitmap1 = bitmap2;
                bitmap2 = bitmapTemp;
            }

            AnaglyphMode anaglyphMode = (AnaglyphMode)cobAnaglyphMode.SelectedItem;
            
            Anaglyph anaglyph = new Anaglyph();
            anaglyph.Mode = AnaglyphMode.Optimized;

            UnsafeBitmap anaglyphProcess = anaglyph.Process(bitmap1, bitmap2);
            anaglyphProcess.UnlockImage();

            m_AnaglyphBitmapResult = anaglyphProcess.InternalBitmap;
            pictureBoxResult.BackgroundImage = m_AnaglyphBitmapResult;
        }

        /// <summary>
        /// Handles the Click event of the cbRightRight control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void cbRightRight_Click(object sender, EventArgs e)
        {

        }

        /// <summary>
        /// Handles the Click event of the cbLeftAnaglyph control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void cbLeftAnaglyph_Click(object sender, EventArgs e)
        {
            ExtractAnaglyph(pictureBoxLeft.BackgroundImage);
        }

        /// <summary>
        /// Handles the Click event of the cbRightAnaglyph control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void cbRightAnaglyph_Click(object sender, EventArgs e)
        {
            ExtractAnaglyph(pictureBoxRight.BackgroundImage);
        }

        /// <summary>
        /// Handles the Click event of the cbLeftLeft control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void cbLeftLeft_Click(object sender, EventArgs e)
        {
            Size size = pictureBoxLeft.BackgroundImage.Size;
            pictureBoxLeft.BackgroundImage = BitmapFunctions.GetPicturePart(pictureBoxLeft.BackgroundImage, new Rectangle(0,0,size.Width-Convert.ToInt16(tbLeft.Text),size.Height));
        }

        /// <summary>
        /// Handles the Click event of the cbLeftRight control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void cbLeftRight_Click(object sender, EventArgs e)
        {
            Size size = pictureBoxLeft.BackgroundImage.Size;
            pictureBoxLeft.BackgroundImage = BitmapFunctions.GetPicturePart(pictureBoxLeft.BackgroundImage, new Rectangle(0, 0, size.Width + Convert.ToInt16(tbLeft.Text), size.Height));
        }

        /// <summary>
        /// Handles the Click event of the cbLeftDown control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void cbLeftDown_Click(object sender, EventArgs e)
        {

        }

        /// <summary>
        /// Handles the Click event of the cbRightLeft control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void cbRightLeft_Click(object sender, EventArgs e)
        {

        }

        private void cbRightUp_Click(object sender, EventArgs e)
        {

        }

        private void cbRightDown_Click(object sender, EventArgs e)
        {

        }

        /// <summary>
        /// Handles the Click event of the cbLoadPictureLeft control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void cbLoadPictureLeft_Click(object sender, EventArgs e)
        {
            OpenFileDialog fd = new OpenFileDialog();
            if (fd.ShowDialog() != DialogResult.OK) return;
            Image image = Image.FromFile(fd.FileName);
            m_sLeft = fd.FileName;
            pictureBoxLeft.BackgroundImage = image;
        }

        /// <summary>
        /// Handles the Click event of the cbLoadPicture control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void cbLoadPictureRight_Click(object sender, EventArgs e)
        {
            OpenFileDialog fd = new OpenFileDialog();
            if (fd.ShowDialog() != DialogResult.OK) return;
            Image image = Image.FromFile(fd.FileName);
            m_sRight = fd.FileName;
            pictureBoxRight.BackgroundImage = image;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (chkbMatchSize.Checked)
            {
                pictureBoxLeft.BackgroundImage = BitmapFunctions.Resize((Bitmap)pictureBoxLeft.BackgroundImage, pictureBoxRight.BackgroundImage.Size);
            }
            else
            {
                if (pictureBoxLeft.BackgroundImage.Size != pictureBoxRight.BackgroundImage.Size)
                {
                    MessageBox.Show("Pics are not of same size!");
                    return;
                }
            }
            ExtractAnaglyph(pictureBoxLeft.BackgroundImage, pictureBoxRight.BackgroundImage);
        }

        private void cbReloadLeft_Click(object sender, EventArgs e)
        {
            pictureBoxLeft.BackgroundImage = Image.FromFile(m_sLeft);
        }

        private void cbReloadRight_Click(object sender, EventArgs e)
        {
            pictureBoxRight.BackgroundImage = Image.FromFile(m_sRight);
        }
    }
}
